﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_HeThong_SupportOnline_Info : System.Web.UI.UserControl
{
    TblSupportOnlineManager _TblSupportOnlineManager = new TblSupportOnlineManager();
    public int SupportID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { SupportID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { SupportID = 0; }

            FunctionExtend.LoadTypeSupport(ddlTypeNick);

            if (SupportID == 0) //Add cate
            {
                lblTitle.Text = "Thêm supportOnline";
                Toolbox1.ShowDel = false;
                Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa thông tin supportOnline: " + SupportID;
                BindData(SupportID);
            }
        }
    }


    //load chi tiet san pham
    protected void BindData(int ID)
    {
        TblSupportOnlineEntity _TblSupportOnlineEntity = _TblSupportOnlineManager.SelectOne(ID);
        if (_TblSupportOnlineEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        txtID.Text = ID.ToString();
        hidID.Value = ID.ToString();
        txtHoten.Text = Server.HtmlDecode(_TblSupportOnlineEntity.HoTen);
        txtEmail.Text = Server.HtmlDecode(_TblSupportOnlineEntity.Email);
        txtChucVu.Text = Server.HtmlDecode(_TblSupportOnlineEntity.ChucVu);
        txtPhone.Text = Server.HtmlDecode(_TblSupportOnlineEntity.Phone);
        txtNick.Text = Server.HtmlDecode(_TblSupportOnlineEntity.NickSupport);
        ddlTypeNick.SelectedValue = _TblSupportOnlineEntity.TypeSupport;
        chkStatus.Checked = Convert.ToBoolean(_TblSupportOnlineEntity.TrangThai);

        if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.Icon))
        {
            chkIcon.Checked = true;
            chkIcon_CheckedChanged(new object(), new EventArgs());
            FileUpload1.FilePath = _TblSupportOnlineEntity.Icon;
            img_news.Visible = lblDelete.Visible = true;
            imgbtn_Delete.Enabled = true;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
            img_news.ImageUrl = _TblSupportOnlineEntity.Icon;
        }
        else
        {
            chkIcon.Checked = false;
            chkIcon_CheckedChanged(new object(), new EventArgs());
            FileUpload1.FilePath = "";
            img_news.Visible = lblDelete.Visible = false;
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_Hidden.gif";
        }

        if(_TblSupportOnlineEntity.Size=="1")
            rbo16.Checked=true;
        else if(_TblSupportOnlineEntity.Size=="2")
            rbo32.Checked=true;
        else if(_TblSupportOnlineEntity.Size=="3")
            rbo64.Checked=true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblSupportOnlineManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtHoten.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tên !</span>"; txtHoten.Focus(); return; }
        if (txtNick.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập nick !</span>"; txtNick.Focus(); return; }
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/SupportOnlineEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        TblSupportOnlineEntity _TblSupportOnlineEntity = new TblSupportOnlineEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblSupportOnlineEntity.Id = Convert.ToInt32(hidID.Value);

        //_TblSupportOnlineEntity.TruyCap = 1;
        _TblSupportOnlineEntity.HoTen = Server.HtmlEncode(txtHoten.Text);
        _TblSupportOnlineEntity.ChucVu = Server.HtmlEncode(txtChucVu.Text);
        _TblSupportOnlineEntity.Email = Server.HtmlEncode(txtEmail.Text);
        _TblSupportOnlineEntity.Phone = Server.HtmlEncode(txtPhone.Text);
        _TblSupportOnlineEntity.NickSupport = Server.HtmlEncode(txtNick.Text);
        _TblSupportOnlineEntity.TypeSupport = Server.HtmlEncode(ddlTypeNick.SelectedValue);
        _TblSupportOnlineEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblSupportOnlineEntity.TrangThai = chkStatus.Checked;

        if (rbo16.Checked)
            _TblSupportOnlineEntity.Size = "1";
        else if (rbo32.Checked)
            _TblSupportOnlineEntity.Size = "2";
        else if (rbo64.Checked)
            _TblSupportOnlineEntity.Size = "3";

        //--tu chon icon
        if(chkIcon.Checked)
        {
            if (!string.IsNullOrEmpty(FileUpload1.FilePath))
                _TblSupportOnlineEntity.Icon = FileUpload1.FilePath;
            else
                _TblSupportOnlineEntity.Icon = null;
        }

        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblSupportOnlineManager.Update(_TblSupportOnlineEntity);
        }
        else
        {
            _TblSupportOnlineManager.Insert(_TblSupportOnlineEntity);
            hidID.Value = _TblSupportOnlineEntity.Id.ToString();
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblSupportOnlineEntity _TblSupportOnlineEntity = _TblSupportOnlineManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblSupportOnlineEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.Icon))
                {
                    string defaultImage = Server.MapPath(_TblSupportOnlineEntity.Icon);
                    try { File.Delete(defaultImage); }
                    catch { }
                    _TblSupportOnlineEntity.Icon = "";
                    _TblSupportOnlineManager.Update(_TblSupportOnlineEntity);
                }
            }
        }
        BindData(Convert.ToInt32(hidID.Value));
    }
    protected void chkIcon_CheckedChanged(object sender, EventArgs e)
    {
        if (chkIcon.Checked)
        {
            UploadIcon.Visible = true;
            chkIcon.Text = "về mặc định";
        }
        else
        {
            UploadIcon.Visible = false;
            chkIcon.Text = "Chọn icon";
        }
    }
}